"""
@File    :   build_signal_sample.py
@Time    :   2024/03/19 18:06:41
@Author  :   glx 
@Version :   1.0
@Contact :   18095542g@connect.polyu.hk
@Desc    :   None
"""

# here put the import lib
from excel2json import *
import numpy as np
import random
from copy import deepcopy


def build_fast_signal_sample(start, var, tan_degree, step: int) -> List[float]:
    """
    input: tan_degree: degree , step
    output: signal sample"""
    tan_value = np.tan(np.deg2rad(tan_degree))
    return list(
        start + tan_value * i + np.random.normal(0, var) for i in range(1, step + 1)
    )


def build_turb_signal_sample(start, diff, var, step: int) -> List[float]:
    """
    input: diff, step
    output: signal sample"""

    return list(start + diff * i + np.random.normal(0, var) for i in range(1, step + 1))


def build_total_signal_sample(
    start: float,
    tan_var: float,
    diff_var: float,
    tan_degree_list: List[float],
    diff_list: List[float],
    tan_step: int,
    diff_step,
) -> List[float]:
    # tan_num = len(tan_degree_list)
    # diff_num = len(diff_list)

    # build the signal sample information list
    tan_degree_list = [(i, 1, tan_step) for i in tan_degree_list]  # i means tan_degree
    diff_list = [
        (i, 2, diff_step) for i in diff_list
    ]  # i means diff value, 2 means diff
    mix_list = tan_degree_list + diff_list
    random.shuffle(mix_list)

    singal = [0]  # start signal
    for info in mix_list:
        start = singal[-1]
        if info[1] == 1:
            singal.extend(
                deepcopy(
                    build_fast_signal_sample(
                        start=start, var=tan_var, tan_degree=info[0], step=info[2]
                    )
                )
            )
        else:
            singal.extend(
                deepcopy(
                    build_turb_signal_sample(
                        start=start, diff=info[0], var=diff_var, step=info[2]
                    )
                )
            )

    return singal[1:]


def generate_tan_list(fast_num, stabel_num, static_num, threshold):
    tan_degree_list = []
    tan_degree_list.extend(
        [90 - random.uniform(-threshold[1], threshold[1]) for i in range(fast_num)]
    )

    tan_degree_list.extend(
        [
            threshold[1] - random.uniform(-threshold[0], threshold[0])
            for i in range(stabel_num)
        ]
    )

    tan_degree_list.extend(
        [random.uniform(-threshold[0], threshold[0]) for i in range(static_num)]
    )

    return [i for i in tan_degree_list]


if __name__ == "__main__":
    import pandas as pd
    import matplotlib.pyplot as plt
    import matplotlib

    matplotlib.use("TkAgg")
    # random.seed(0)

    static_tan_threshold = 2
    stabel_tan_range_threshold = 20

    diff_range = (10, 12)
    tan_degree_list = generate_tan_list(
        20, 10, 10, (static_tan_threshold, stabel_tan_range_threshold)
    )
    # build the signal sample
    data = build_total_signal_sample(
        start=0,
        tan_var=3,
        diff_var=5,
        tan_degree_list=tan_degree_list,
        diff_list=[20],
        tan_step=50,
        diff_step=50,
    )

    plt.plot(data)
    plt.show()

    # save the data to csv
    df = pd.DataFrame(data)
    df.to_csv(
        rf"data\csv\data_{static_tan_threshold}_{stabel_tan_range_threshold}.csv",
        index=False,
        header=False,
    )
